package com.jeff.client;

import java.io.File;
import java.io.PrintWriter;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jeff.client.util.Constans;

public class Login extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		Button button_login = (Button) findViewById(R.id.button_login);
        button_login.setOnClickListener(new View.OnClickListener() {
        	private String userId = null;
        	private String passWd = null;
        	
			@Override
			public void onClick(View v) {
        		Log.i("yao", "clicked me");
        		TextView textViewUserId = (TextView)findViewById(R.id.editText_userId);
        		userId = textViewUserId.getText().toString();
        		TextView textViewPassWd = (TextView)findViewById(R.id.editText_passWord);
        		passWd = textViewPassWd.getText().toString();
        		if(userId == null || userId.length()==0) {
        			Log.i("yao", "userId is empty");
        			Toast.makeText(getApplicationContext(), 
				              "userId is empty", 
				              Toast.LENGTH_LONG).show(); 
        			return;
        		}
        		if(passWd == null || passWd.length()==0) {
        			Log.i("yao", "passWd is empty");
        			Toast.makeText(getApplicationContext(), 
				              "passWd is empty", 
				              Toast.LENGTH_LONG).show(); 
        			return;
        		}
        		
        		if(loginToServer()) {
        			persistPhone();
        			Intent intent = getIntent(); 
        			intent.putExtra("login", "true");
        			finish();
        		}
        		Log.i("yao", "end click");
			}
			
			boolean loginToServer() {
				if(!PhoneNumberUtils.isGlobalPhoneNumber(userId)) {
					Toast.makeText(getApplicationContext(), 
				              "not a valid number, please have a check", 
				              Toast.LENGTH_LONG).show(); 
					return false;
				}
				EchoClient echoClient = new EchoClient(Constans.LISTEN_ADMIN_IP, Constans.LISTEN_ADMIN_PORT);
				String sendMsg = "login\t"+userId+"\t"+passWd;
				echoClient.sendMsg(sendMsg);
				String recvMsg = echoClient.receiveMsg();
				if(sendMsg.equals(recvMsg)) {
					return true;
				}
				return false;
			}
			
			boolean persistPhone() {
				try {
					PrintWriter pw = new PrintWriter(new File(Constans.PERSIST_FILE));
					Calendar rightNow = Calendar.getInstance();
					int year = rightNow.get(Calendar.YEAR);
					int month = rightNow.get(Calendar.MONTH) + 1;
					int day = rightNow.get(Calendar.DAY_OF_MONTH);
					int hour = rightNow.get(Calendar.HOUR);
					int minute = rightNow.get(Calendar.MINUTE);
					int second = rightNow.get(Calendar.SECOND);
					String time = "" + year + "-" + month + "-" + day +"," + hour + ":" + minute + ":" + second;
					pw.println(userId + "\t" + time);
					pw.close();
				} catch (Exception e) {
					e.printStackTrace();
				}

				return true;
			}
		});
	}
}
